﻿using System.Collections.Generic;
using System.Text;
using UnityEngine;

namespace LightGameLibs
{
    public static class StringUtility
    {
        public static string RemoveSuffix(this string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return fileName;
            }

            int count = fileName.LastIndexOf('.');
            if (-1 == count)
            {
                return fileName;
            }

            return fileName.Remove(count, fileName.Length - count);
        }

        public static string ToUpperFolder(this string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return path;
            }

            int count = path.LastIndexOf('/');
            if (-1 == count)
            {
                return path;
            }

            return path.Remove(count, path.Length - count); ;
        }

        public static string DirecRelativeToProject(this string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return path;
            }

            int start = path.IndexOf("Assets");
            if (-1 == start || 0 == start)
            {
                return path;
            }

            return path.Remove(0, start);
        }

        public static string Add(this string target, string toAdd)
        {
            var sb = new StringBuilder(target.Length + toAdd.Length);
            sb.Append(target);
            sb.Append(toAdd);
            return sb.ToString();
        }

        public static string Format(string format, string arg0)
        {
            var sb = new StringBuilder(format.Length + arg0.Length);
            sb.AppendFormat(format, arg0);
            return sb.ToString();
        }

        public static string Format(string format, string arg0, string arg1)
        {
            var sb = new StringBuilder(format.Length + arg0.Length + arg1.Length);
            sb.AppendFormat(format, arg0, arg1);
            return sb.ToString();
        }

        public static string Format(string format, string arg0, string arg1, string arg2)
        {
            var sb = new StringBuilder(format.Length + arg0.Length + arg1.Length + arg2.Length);
            sb.AppendFormat(format, arg0, arg1, arg2);
            return sb.ToString();
        }

        public static string Format(string format, params string[] args)
        {
            int totalLength = format.Length;
            for (int i = 0, length = args.Length; i < length; i++)
            {
                totalLength += args[i].Length;
            }

            var sb = new StringBuilder(totalLength);
            sb.AppendFormat(format, args);
            return sb.ToString();
        }
    }
}
